<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { qrcodeApi } from '../api'

const router = useRouter()

// 二维码列表数据
const qrcodes = ref([])
// 分页信息
const pagination = ref({
  current: 1,
  size: 10,
  total: 0,
  pages: 0
})
// 加载状态
const loading = ref(false)
// 错误信息
const errorMsg = ref('')

// 获取二维码列表
const fetchQRCodes = async (page = 1) => {
  loading.value = true
  errorMsg.value = ''
  
  try {
    const res = await qrcodeApi.getCurrentUserQRCodes(page, pagination.value.size)
    qrcodes.value = res.data.list
    // 确保不显示图片路径，防止刷新页面时请求图片导致404错误
    qrcodes.value.forEach(qrcode => {
      qrcode.imagePath = null
    })
    pagination.value = {
      current: res.data.current,
      size: res.data.size,
      total: res.data.total,
      pages: res.data.pages
    }
  } catch (error) {
    // 如果发生404错误，显示友好的错误信息，但不影响页面显示
    errorMsg.value = '获取二维码列表失败: ' + (error.message || '未知错误')
    console.error('获取二维码列表失败:', error)
    // 设置空列表，避免页面崩溃
    qrcodes.value = []
  } finally {
    loading.value = false
  }
}

// 删除二维码
const handleDelete = async (id) => {
  if (!confirm('确定要删除这个二维码吗？此操作不可恢复。')) {
    return
  }
  
  try {
    await qrcodeApi.deleteQRCode(id)
    // 删除成功后刷新列表
    fetchQRCodes(pagination.value.current)
  } catch (error) {
    alert('删除失败: ' + (error.message || '未知错误'))
    console.error('删除二维码失败:', error)
  }
}

// 跳转到创建二维码页面
const goToCreate = () => {
  router.push('/dashboard/qrcode/create')
}

// 跳转到编辑二维码页面
const goToEdit = (id) => {
  router.push(`/dashboard/qrcode/edit/${id}`)
}

// 处理分页变化
const handlePageChange = (page) => {
  fetchQRCodes(page)
}

// 组件挂载时获取二维码列表
onMounted(() => {
  fetchQRCodes()
})
</script>

<template>
  <div class="qrcode-management">
    <div class="page-header">
      <h2>我的二维码</h2>
      <button @click="goToCreate" class="create-btn">创建新二维码</button>
    </div>
    
    <!-- 错误提示 -->
    <div v-if="errorMsg" class="error-message">
      {{ errorMsg }}
    </div>
    
    <!-- 加载中 -->
    <div v-if="loading" class="loading-spinner">
      <div class="spinner"></div>
      <p>加载中...</p>
    </div>
    
    <!-- 二维码列表 -->
    <div v-else-if="qrcodes.length > 0" class="qrcode-list">
      <div class="qrcode-grid">
        <div v-for="qrcode in qrcodes" :key="qrcode.id" class="qrcode-card">
          <!-- 不显示图片详情，避免404错误 -->
          <div class="qrcode-info">
            <h3 class="qrcode-title">{{ qrcode.title || '未命名二维码' }}</h3>
            <p class="qrcode-content">{{ qrcode.content }}</p>
            <p v-if="qrcode.description" class="qrcode-description">{{ qrcode.description }}</p>
            <p class="qrcode-date">创建于: {{ new Date(qrcode.createTime).toLocaleString() }}</p>
          </div>
          
          <div class="qrcode-actions">
            <button @click="goToEdit(qrcode.id)" class="edit-btn">编辑</button>
            <button @click="handleDelete(qrcode.id)" class="delete-btn">删除</button>
          </div>
        </div>
      </div>
      
      <!-- 分页 -->
      <div v-if="pagination.pages > 1" class="pagination">
        <button 
          :disabled="pagination.current === 1" 
          @click="handlePageChange(pagination.current - 1)" 
          class="page-btn"
        >
          上一页
        </button>
        
        <span class="page-info">
          {{ pagination.current }} / {{ pagination.pages }}
        </span>
        
        <button 
          :disabled="pagination.current === pagination.pages" 
          @click="handlePageChange(pagination.current + 1)" 
          class="page-btn"
        >
          下一页
        </button>
      </div>
    </div>
    
    <!-- 空状态 -->
    <div v-else class="empty-state">
      <p>您还没有创建任何二维码</p>
      <button @click="goToCreate" class="create-btn">创建第一个二维码</button>
    </div>
  </div>
</template>

<style scoped>
.qrcode-management {
  background: var(--card-bg);
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--spacing-xl);
  margin: var(--spacing-md);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--spacing-xl);
  padding-bottom: var(--spacing-lg);
  border-bottom: 2px solid var(--border-color);
  position: relative;
}

.page-header::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 60px;
  height: 2px;
  background: var(--gradient-primary);
  border-radius: 1px;
}

h2 {
  margin: 0;
  color: var(--dark-color);
  font-weight: 700;
  font-size: 1.8rem;
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.create-btn {
  background: var(--gradient-primary);
  color: white;
  border: none;
  padding: var(--spacing-sm) var(--spacing-lg);
  border-radius: var(--border-radius-md);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-md);
  position: relative;
  overflow: hidden;
}

.create-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  transition: left 0.5s;
}

.create-btn:hover::before {
  left: 100%;
}

.create-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.error-message {
  background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
  color: var(--danger-color);
  padding: var(--spacing-md);
  border-radius: var(--border-radius-md);
  margin-bottom: var(--spacing-lg);
  border-left: 4px solid var(--danger-color);
  box-shadow: var(--shadow-sm);
  font-weight: 500;
}

.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-xxl) 0;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(var(--primary-rgb), 0.1);
  border-radius: 50%;
  border-top-color: var(--primary-color);
  animation: spin 1s ease-in-out infinite;
  margin-bottom: var(--spacing-md);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-spinner p {
  color: var(--text-secondary);
  font-weight: 500;
  margin: 0;
}

.qrcode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: var(--spacing-xl);
  margin-bottom: var(--spacing-xl);
}

.qrcode-card {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-lg);
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-md);
  position: relative;
}

.qrcode-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--gradient-primary);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.qrcode-card:hover {
  transform: translateY(-8px);
  box-shadow: var(--shadow-xl);
}

.qrcode-card:hover::before {
  transform: scaleX(1);
}

.qrcode-image {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  padding: var(--spacing-md);
  position: relative;
}

.qrcode-image::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="%23e2e8f0" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.qrcode-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--border-radius-sm);
  box-shadow: var(--shadow-sm);
  position: relative;
  z-index: 1;
}

.no-image {
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
  position: relative;
  z-index: 1;
}

.qrcode-info {
  padding: var(--spacing-lg);
}

.qrcode-title {
  margin: 0 0 var(--spacing-sm) 0;
  font-size: 1.2rem;
  color: var(--dark-color);
  font-weight: 600;
  line-height: 1.4;
}

.qrcode-content {
  margin: 0 0 var(--spacing-sm) 0;
  font-size: 0.9rem;
  color: var(--text-secondary);
  word-break: break-all;
  line-height: 1.5;
  background: var(--bg-light);
  padding: var(--spacing-sm);
  border-radius: var(--border-radius-sm);
  border-left: 3px solid var(--primary-color);
}

.qrcode-description {
  margin: 0 0 var(--spacing-sm) 0;
  font-size: 0.9rem;
  color: var(--text-secondary);
  line-height: 1.5;
}

.qrcode-date {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
}

.qrcode-date::before {
  content: '📅';
  margin-right: var(--spacing-xs);
}

.qrcode-actions {
  display: flex;
  gap: var(--spacing-sm);
  padding: var(--spacing-md) var(--spacing-lg);
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border-top: 1px solid var(--border-color);
}

.edit-btn, .delete-btn {
  flex: 1;
  padding: var(--spacing-sm) var(--spacing-md);
  border: none;
  border-radius: var(--border-radius-md);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.edit-btn {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
  box-shadow: var(--shadow-sm);
}

.edit-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  transition: left 0.5s;
}

.edit-btn:hover::before {
  left: 100%;
}

.edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.delete-btn {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  color: white;
  box-shadow: var(--shadow-sm);
}

.delete-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  transition: left 0.5s;
}

.delete-btn:hover::before {
  left: 100%;
}

.delete-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: var(--spacing-xxl);
  gap: var(--spacing-md);
}

.page-btn {
  padding: var(--spacing-sm) var(--spacing-lg);
  border: 2px solid var(--border-color);
  background: var(--card-bg);
  color: var(--dark-color);
  cursor: pointer;
  border-radius: var(--border-radius-md);
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}

.page-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: var(--gradient-primary);
  transition: left 0.3s ease;
  z-index: -1;
}

.page-btn:hover:not(:disabled) {
  color: white;
  border-color: var(--primary-color);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.page-btn:hover:not(:disabled)::before {
  left: 0;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.page-info {
  margin: 0;
  color: var(--text-secondary);
  font-weight: 600;
  padding: var(--spacing-sm) var(--spacing-md);
  background: var(--bg-light);
  border-radius: var(--border-radius-md);
  border: 2px solid var(--border-color);
}

.empty-state {
  text-align: center;
  padding: var(--spacing-xxl) 0;
  color: var(--text-secondary);
}

.empty-state p {
  margin-bottom: var(--spacing-xl);
  font-size: 1.1rem;
  font-weight: 500;
}

.empty-state::before {
  content: '📱';
  font-size: 4rem;
  display: block;
  margin-bottom: var(--spacing-lg);
  opacity: 0.5;
}
</style>